home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / demosrc / vectball / vectball.cpp < prev    next >
C/C++ Source or Header  |  1994-01-10  |  10KB  |  404 lines

  1. // VECTBALL.CPP
  2. // Vector Ball routines
  3. // coded by Tumblin / Bodies In Motion
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include "XLib_all.h"
  10.  
  11. // palette data for vector balls
  12. char vbpalette[24*3]=
  13. {
  14.      0, 0, 0, // color # 0
  15.      0, 0,12, // color # 1
  16.      4, 4,12, // color # 2
  17.      4, 4,16, // color # 3
  18.      4, 4,20, // color # 4
  19.      4, 4,24, // color # 5
  20.      8, 8,24, // color # 6
  21.      8, 8,28, // color # 7
  22.      8, 8,32, // color # 8
  23.      8, 8,36, // color # 9
  24.     12,12,40, // color # 10
  25.     12,12,44, // color # 11
  26.     12,12,48, // color # 12
  27.     12,12,52, // color # 13
  28.     16,16,48, // color # 14
  29.     16,16,52, // color # 15
  30.     16,16,56, // color # 16
  31.     20,20,56, // color # 17
  32.     20,20,60, // color # 18
  33.     24,24,60, // color # 19
  34.     28,28,60, // color # 20
  35.     32,32,60, // color # 21
  36.     36,36,60, // color # 22
  37.     60,60,60, // color # 23
  38. };
  39.  
  40.  
  41.  
  42. // This is a Mode-X planar bitmap
  43. char vbbitmap[24*31+2] =
  44. {
  45.     // width in bytes (4 pixel groups), height in pixels
  46.     6,31,
  47.     // plane 0
  48.     0,  0,  0, 10,  0,  0,
  49.     0,  0,  9, 12,  0,  0,
  50.     0,  0, 10, 12, 12,  0,
  51.     0,  5, 11, 13, 15,  0,
  52.     0,  7, 11, 15, 15,  0,
  53.     0,  7, 11, 15, 16,  0,
  54.     0,  8, 11, 15, 16, 11,
  55.     0,  8, 11, 15, 16, 12,
  56.     0,  8, 11, 15, 16, 12,
  57.     0,  8, 11, 15, 16, 12,
  58.     2,  8, 11, 19, 15, 12,
  59.     2,  8, 10, 22, 15, 12,
  60.     2,  7, 10, 21, 13, 12,
  61.     2,  7, 10, 15, 12, 11,
  62.     2,  7,  9, 11, 12, 11,
  63.     2,  7,  9, 11, 11, 10,
  64.     2,  6,  9, 10, 11, 10,
  65.     2,  5,  8, 10, 10,  9,
  66.     2,  5,  8,  9, 10,  9,
  67.     1,  4,  7,  9,  9,  8,
  68.     1,  4,  7,  8,  9,  8,
  69.     1,  3,  6,  8,  8,  7,
  70.     0,  3,  5,  7,  8,  6,
  71.     0,  3,  4,  7,  7,  4,
  72.     0,  2,  4,  5,  6,  4,
  73.     0,  2,  3,  4,  5,  3,
  74.     0,  1,  3,  4,  4,  0,
  75.     0,  1,  2,  3,  3,  0,
  76.     0,  1,  2,  3,  3,  0,
  77.     0,  0,  1,  2,  2,  0,
  78.     0,  0,  1,  1,  0,  0,
  79.     // plane 1
  80.     0,  0,  8, 10,  0,  0,
  81.     0,  0, 10, 12,  0,  0,
  82.     0,  5, 11, 13, 10,  0,
  83.     0,  7, 11, 15, 12,  0,
  84.     0,  8, 12, 15, 15,  0,
  85.     0,  8, 12, 16, 15,  0,
  86.     0,  9, 12, 16, 16,  0,
  87.     2,  9, 12, 15, 16,  8,
  88.     3,  9, 12, 15, 16, 10,
  89.     3,  9, 11, 16, 15, 11,
  90.     4,  9, 11, 22, 15, 11,
  91.     4,  8, 11, 23, 15, 11,
  92.     4,  8, 11, 23, 13, 11,
  93.     3,  8, 10, 20, 12, 10,
  94.     3,  8, 10, 12, 12, 10,
  95.     3,  7, 10, 11, 11, 10,
  96.     3,  7,  9, 11, 11,  9,
  97.     3,  7,  9, 10, 10,  9,
  98.     2,  5,  8, 10, 10,  8,
  99.     2,  5,  8,  9,  9,  7,
  100.     2,  4,  7,  8,  9,  7,
  101.     2,  4,  7,  8,  8,  5,
  102.     1,  4,  6,  7,  7,  4,
  103.     1,  3,  5,  7,  7,  3,
  104.     1,  3,  4,  5,  5,  2,
  105.     0,  2,  4,  5,  4,  0,
  106.     0,  2,  3,  4,  4,  0,
  107.     0,  1,  3,  3,  3,  0,
  108.     0,  1,  2,  3,  2,  0,
  109.     0,  1,  2,  2,  1,  0,
  110.     0,  0,  1,  1,  0,  0,
  111.     // plane 2
  112.     0,  0,  9,  0,  0,  0,
  113.     0,  0, 11, 12,  0,  0,
  114.     0,  8, 11, 13,  0,  0,
  115.     0,  9, 12, 15, 11,  0,
  116.     0,  9, 12, 16, 12,  0,
  117.     0,  9, 12, 16, 13,  0,
  118.     4, 10, 12, 16, 15,  0,
  119.     4, 10, 12, 16, 15,  0,
  120.     5, 10, 12, 16, 15,  0,
  121.     5,  9, 12, 16, 15,  0,
  122.     5,  9, 12, 18, 15,  8,
  123.     5,  9, 11, 22, 13,  9,
  124.     5,  9, 11, 22, 12,  9,
  125.     4,  9, 11, 17, 12,  9,
  126.     4,  8, 10, 12, 12,  9,
  127.     4,  8, 10, 11, 11,  8,
  128.     4,  8, 10, 11, 11,  8,
  129.     3,  7,  9, 10, 10,  7,
  130.     3,  7,  9, 10, 10,  7,
  131.     3,  6,  8,  9,  9,  5,
  132.     3,  5,  8,  9,  8,  4,
  133.     2,  4,  7,  8,  8,  3,
  134.     2,  4,  7,  7,  7,  0,
  135.     2,  4,  5,  7,  6,  0,
  136.     1,  3,  5,  6,  5,  0,
  137.     1,  3,  4,  5,  4,  0,
  138.     0,  2,  3,  4,  3,  0,
  139.     0,  2,  3,  3,  3,  0,
  140.     0,  1,  2,  3,  2,  0,
  141.     0,  1,  2,  2,  0,  0,
  142.     0,  0,  1,  1,  0,  0,
  143.     // plane 3
  144.     0,  0, 10,  0,  0,  0,
  145.     0,  8, 11, 11,  0,  0,
  146.     0,  9, 12, 12,  0,  0,
  147.     0, 10, 12, 15,  0,  0,
  148.     4, 10, 13, 16, 10,  0,
  149.     5, 10, 13, 16, 12,  0,
  150.     6, 10, 13, 16, 12,  0,
  151.     7, 10, 13, 16, 13,  0,
  152.     7, 10, 13, 16, 13,  0,
  153.     7, 10, 12, 16, 13,  0,
  154.     7, 10, 12, 15, 13,  0,
  155.     7, 10, 14, 15, 12,  0,
  156.     6,  9, 12, 15, 12,  0,
  157.     6,  9, 11, 12, 12,  0,
  158.     5,  9, 11, 12, 11,  0,
  159.     5,  8, 10, 11, 11,  0,
  160.     4,  8, 10, 11, 10,  0,
  161.     4,  8,  9, 10, 10,  0,
  162.     4,  7,  9, 10,  9,  0,
  163.     4,  7,  9,  9,  9,  0,
  164.     3,  6,  8,  9,  8,  0,
  165.     3,  5,  7,  8,  7,  0,
  166.     3,  4,  7,  8,  7,  0,
  167.     2,  4,  6,  7,  5,  0,
  168.     2,  4,  5,  6,  4,  0,
  169.     1,  3,  4,  5,  4,  0,
  170.     1,  3,  4,  4,  3,  0,
  171.     1,  2,  3,  3,  2,  0,
  172.     0,  2,  3,  3,  0,  0,
  173.     0,  1,  2,  2,  0,  0,
  174.     0,  0,  1,  0,  0,  0
  175. };
  176.  
  177.  
  178.  
  179. // function prototypes
  180. void draw_vector_balls(void);
  181. void create_lookup_tables(void);
  182. void create_vector_object(void);
  183. int main(void);
  184.  
  185. // sin and cos lookup tables
  186. float cosine[360];
  187. float sine[360];
  188.  
  189. // structure for one vector ball
  190. typedef struct
  191. {
  192.     int x;
  193.     int y;
  194.     int z;
  195.     int color;
  196. } VectBallType;
  197.  
  198. // maximum number of balls in this demo's object
  199. int maxballs=8;
  200.  
  201. // start off demo with some random initial angles
  202. int xangle=random(360);
  203. int yangle=random(360);
  204. int zangle=random(360);
  205.  
  206. // the z distance from the center of the universe (0, 0, 0 :-)
  207. int distance=256;
  208. // flag to tell when the object is moving towards or away from you
  209. int direction=1;
  210.  
  211. // origional vector ball object
  212. VectBallType object[8];
  213. // working copy of vector ball object
  214. VectBallType temp[8];
  215. // single dummy vector ball used for swaping
  216. VectBallType dummy;
  217.  
  218. //---------------------------------------------------------------
  219. void create_lookup_tables(void)
  220. {
  221.     int i;
  222.     for(i=0; i<360; i++)
  223.     {
  224.         cosine[i] = cos(i * 3.14159265 / 180);
  225.         sine[i] = sin(i * 3.14159265 / 180);
  226.     }
  227. }
  228.  
  229.  
  230. //---------------------------------------------------------------
  231. void create_vector_object(void)
  232. {
  233.     // This object will look like a cube
  234.     // back top left
  235.     object[0].x=-30;
  236.     object[0].y=-30;
  237.     object[0].z=30;
  238.     // back top right
  239.     object[1].x=30;
  240.     object[1].y=-30;
  241.     object[1].z=31;
  242.     // back bottom left
  243.     object[2].x=-30;
  244.     object[2].y=30;
  245.     object[2].z=32;
  246.     // back bottom right
  247.     object[3].x=30;
  248.     object[3].y=30;
  249.     object[3].z=33;
  250.     // front top left
  251.     object[4].x=-30;
  252.     object[4].y=-30;
  253.     object[4].z=-30;
  254.     // front top right
  255.     object[5].x=30;
  256.     object[5].y=-30;
  257.     object[5].z=-31;
  258.     // front bottom left
  259.     object[6].x=-30;
  260.     object[6].y=30;
  261.     object[6].z=-32;
  262.     // front bottom right
  263.     object[7].x=30;
  264.     object[7].y=30;
  265.     object[7].z=-33;
  266. }
  267.  
  268. void draw_vector_object(void)
  269. {
  270.     int i,j,k,a,b,nx,ny,nz;
  271.  
  272.     // first we have to do all the rotations
  273.     for(i=0;i<8;i++)
  274.     {
  275.         // rotate around the x-axis
  276.         ny=(int)(object[i].y*cosine[xangle]-object[i].z*sine[xangle]);
  277.         nz=(int)(object[i].y*sine[xangle]+object[i].z*cosine[xangle]);
  278.         nx=(int)(object[i].x);
  279.         temp[i].x=nx;
  280.         temp[i].y=ny;
  281.         temp[i].z=nz;
  282.  
  283.         // rotate around the y-axis
  284.         nx=(int)(temp[i].x * cosine[yangle] +temp[i].z * sine[yangle]);
  285.         nz=(int)(-temp[i].x * sine[yangle]+temp[i].z * cosine[yangle]);
  286.         temp[i].x=nx;
  287.         temp[i].z=nz;
  288.  
  289.         // rotate around the z-axis
  290.         nx=(int)(temp[i].x * cosine[zangle]-temp[i].y * sine[zangle]);
  291.         ny=(int)(temp[i].x * sine[zangle]+temp[i].y * cosine[zangle]);
  292.         temp[i].x=nx;
  293.         temp[i].y=ny;
  294.  
  295.         // now we have to push the z coordinates into the view area
  296.         temp[i].z-=distance;
  297.  
  298.         // finally we project & copy the new x,y,z to a temporary array
  299.         temp[i].x=(int)((temp[i].x*256)/temp[i].z+160);
  300.         temp[i].y=(int)((temp[i].y*256)/temp[i].z+120);
  301.         temp[i].color=object[i].color;
  302.     }
  303.  
  304.     // now we need to do z-buffering
  305.     // the z with most positive value should be in lowest index
  306.     // I used bubble sort here because there are only 8 balls :-)
  307.     for(j=0;j<7;j++)
  308.     {
  309.         for(k=j+1;k<8;k++)
  310.         {
  311.             a=temp[j].z;
  312.             b=temp[k].z;
  313.             if(a >= b)
  314.             {
  315.                 // swap the two vector balls
  316.                 dummy=temp[j];
  317.                 temp[j]=temp[k];
  318.                 temp[k]=dummy;
  319.             }
  320.         }
  321.     }
  322.  
  323.     // we're done everything, so draw the object!
  324.     // first we erase the old stuff off the drawing zone
  325.     x_rect_fill(160-70,120-70,160+70,120+70,HiddenPageOffs,0);
  326.     for(i=0;i<8;i++)
  327.     {
  328.         x_put_masked_pbm(temp[i].x-11,temp[i].y-15,HiddenPageOffs,vbbitmap);
  329.     }
  330. }
  331.  
  332.  
  333. //---------------------------------------------------------------
  334. int main(void)
  335. {
  336.     randomize();
  337.     x_text_mode();
  338.     printf("\n\n\nV E C T O R   B A L L S");
  339.     printf("\ncoded by Tumblin / Bodies In Motion  (a.k.a.  Terry Sznober)");
  340.     printf("\n\n\nPress any key to begin demo.  Press a key again to quit demo.");
  341.     getch();
  342.     x_set_mode(X_MODE_320x240,320);
  343.     create_lookup_tables();
  344.     create_vector_object();
  345.     x_put_pal_raw(vbpalette,24,0);
  346.     x_set_doublebuffer(240);
  347.     for(;;)    // do an infinite loop until user hits a key
  348.     {
  349.         if(kbhit())
  350.             break;
  351.         if(direction==1)    // if object is moving away from you,
  352.         {
  353.             distance++;    // move it even further
  354.             if(distance>=600)    // if at the limit, make it come back
  355.             {
  356.                 direction=0;
  357.                 distance=600;
  358.             }
  359.         }
  360.         else    // no, object is moving towards you
  361.         {
  362.             distance--;    // bring it a little closer
  363.             if(distance<=256)    // if at limit, make it go away again
  364.             {
  365.                 direction=1;
  366.                 distance=256;
  367.             }
  368.         }
  369.         // update the angles
  370.         xangle+=3;
  371.         if(xangle>=360)
  372.             xangle=0;
  373.         yangle+=3;
  374.         if(yangle>=360)
  375.             yangle=0;
  376.         zangle-=2;
  377.         if(zangle<=0)
  378.             zangle=359;
  379.         // draw the vector ball object on the hidden screen
  380.         draw_vector_object();
  381.         // show what we just did
  382.         x_page_flip(0,0);
  383.     }
  384.     getch();
  385.     x_text_mode();
  386.  
  387.     // show the greets                                                                   |
  388.     printf("                     Tumblin / Bodies In Motion  greets:\n\n");
  389.     printf("                             The Future Crew\n");
  390.     printf("                                Avalanche\n");
  391.     printf("                         The Emulator Institute\n");
  392.     printf("                                 Triton\n");
  393.     printf("                                 Witan\n");
  394.     printf("                                Silents\n\n");
  395.     printf("                                 Shades\n");
  396.     printf("                              Shadow Hunter\n");
  397.     printf("                                 Gravis\n");
  398.     printf("                     all the guys that helped me on IRC\n");
  399.     printf("                  every coder who ever released source code\n");
  400.     printf("                       and Themie Gouthas for XLib06\n");
  401.  
  402.     return(0);
  403. }
  404.